﻿using System.Windows;
using System.Windows.Controls;
using Core.Infrastructure.AnimationService;
using Core.Infrastructure.Constants;
using Core.Infrastructure.EventArgs;
using Core.Infrastructure.Menu;
using Core.Infrastructure.ScreenManager;
using Core.Infrastructure.Settings;
using Core.Infrastructure.ViewModelBase;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Unity;
using System.Collections.Generic;
using Api.Model;

namespace Core
{
    public class ShellViewModel : ViewModelBase, IShellViewModel
    {
        public ShellViewModel(IEventAggregator eventAggregator, 
            IAnimationService animationService, 
            IUnityContainer container,
            ISettings settings,
            IMenu menu) 
            : base(eventAggregator, animationService, container, settings)
        {
            this.AnimationService = animationService;

            MenuData = menu;
            Loaded = new DelegateCommand<object>(OnLoad);
            Unloaded = new DelegateCommand<object>(OnUnload);
            ToogleFullScreen = new DelegateCommand<object>(ChangeFullScreen);
            this.MenuClickCommand = new DelegateCommand<object>(MenuItemClicked);
            this.ShowArticlesByTagCommand = new DelegateCommand<Tag>(ShowArticlesByTag);
            SubscribeToEvents();
        }

        private void SubscribeToEvents()
        {
            EventAggregator.GetEvent<TagsLoadedEvent>().Subscribe(RefreshTags);
        }

        public void RefreshTags(IEnumerable<Tag> tags)
        {
            this.Tags = tags;
        }

        public void MenuItemClicked(object obj)
        {
            if (obj == null) return;

            var menuItem = obj as MenuItem;
            if (menuItem == null) return;
            ActivateScreen(menuItem.ScreenType);
        }

        public void ActivateScreen(ScreenType screen)
        {
            var screenEventArgs = new ScreenEventArgs
            {
                ScreenType = screen,
                RegionName = RegionConstants.RegionMainArea,
                PerformAnimation = true
            };
            this.EventAggregator.GetEvent<ScreenActivateEvent>().Publish(screenEventArgs);
        }

        public void ChangeFullScreen(object obj)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        public override void OnLoad(object obj)
        {
            this.IsActive = true;
        }

        public override void OnUnload(object obj)
        {
            this.IsActive = false;
        }

        public DelegateCommand<Tag> ShowArticlesByTagCommand { get; private set; }
        public DelegateCommand<object> MenuClickCommand { get; protected set; }
        public DelegateCommand<object> ToogleFullScreen { get; set; } 

        private IMenu menuData;

        public IMenu MenuData
        {
            get { return menuData; }
            set
            {
                menuData = value;
                base.RaisePropertyChanged(() => this.MenuData);
            }
        }

        public override bool IsActive { get; set; }

        public bool ShowMenu { get; set; }

        public bool FullScreen { get; set; }

        private IEnumerable<Tag> tags;

        public IEnumerable<Tag> Tags
        {
            get { return tags; }
            set 
            { 
                tags = value;
                base.RaisePropertyChanged(() => this.Tags);
            }
        }

        public void ShowArticlesByTag(Tag tag)
        {
            var screenEventArgs = new ScreenEventArgs
            {
                ScreenType = ScreenType.ArticlesScreen,
                RegionName = RegionConstants.RegionMainArea,
                PerformAnimation = true,
                NavigationArgs = tag,
            };
            this.EventAggregator.GetEvent<ScreenActivateEvent>().Publish(screenEventArgs);
        }
    }
}
